const app = getApp();
import Toast, {
    hideToast
} from 'tdesign-miniprogram/toast/index';
Page({
    data: {
        pageParams: {
            pageNo: 1,
            pageSize: 8,
            params: {
                name: '',
                type: null,
                beneficialPosition: ''
            }
        },
        list: [],
        /** 是否可以加载更多 */
        hasMore: true,
        imageProps: {
            mode: 'aspectFill'
        },
        seachValue: '',
        actionText: ''
    },

    onLoad() {
        this.findPage(false);
    },
    onShow() {
        this.getTabBar().init();
    },
    /**
     * 分页查询
     * @param {*} refrensh 是否刷新
     */
    findPage(refrensh) {
        this.showToast('加载中...', true);
        app.httpPost('mini/index/list', this.data.pageParams).then((res) => {
            hideToast({
                context: this,
                selector: '#t-toast',
            });
            const {
                current,
                pages,
                records
            } = res.data;
            if (current >= pages) {
                this.setData({
                    hasMore: false
                })
            }
            if (records && records.length > 0) {
                const result = records.filter(item => {
                    item.imageUrl = app.fileView(item.applicableAge);
                    item.url = '/pages/index/info/info?id=' + item.id;
                    return true;
                });
                let list;
                if (refrensh) {
                    list = result;
                } else {
                    list = this.data.list.concat(result);
                }
                console.log(list);
                this.setData({
                    list
                });
            } else {
                if (refrensh) {
                    this.setData({
                        list: []
                    });
                }
                this.showToast('没有更多的数据了');
            }
        });
    },
    /**
     * 监听用户下拉刷新事件
     */
    onPullDownRefresh() {
        const pageParams = {
            pageNo: 1,
            pageSize: 8,
            params: {
                name: '',
                type: null,
                introduction: ''
            }
        };
        this.setData({
            hasMore: true,
            pageParams
        });
        // 执行查询方法
        this.findPage(true);
        wx.stopPullDownRefresh()
    },
    /**
     * 监听用户上拉
     */
    onReachBottom() {
        if (!this.data.hasMore) {
            this.showToast('数据已经见底了');
            return;
        }
        this.data.pageParams.pageNo = this.data.pageParams.pageNo + 1;
        this.findPage(false);
    },
    showToast(str, isClose = false) {
        const obj = {
            context: this,
            selector: '#t-toast',
            message: `${str}~`,
        };
        if (isClose) {
            obj.duration = -1;
        }
        Toast(obj);
    },

    changeHandle(e) {
        const { value } = e.detail;
        this.setData({ 
            seachValue: value,
        });
    },

    focusHandle() {
        this.setData({
            actionText: '取消',
        });
    },

    blurHandle() {
        this.setData({
            actionText: '',
        });
    },

    actionHandle() {
        this.setData({
            seachValue: '',
            actionText: '',
        });
    },

    onSearch() {
        this.data.pageParams.params.name = this.data.seachValue;
        this.data.pageParams.params.beneficialPosition = this.data.seachValue;
        this.findPage(true);
    }
})